package com.loong.seller.controller.backend;

import com.loong.common.domain.BaseQuery;
import com.loong.common.mvc.BaseBackendController;
import com.loong.common.rest.Result;
import com.loong.common.utils.TransformUtils;
import com.loong.common.validator.group.CommonInsert;
import com.loong.common.validator.group.CommonUpdate;
import com.loong.common.validator.group.CommonValidate;
import com.loong.seller.common.backend.param.DeliveryWaysParam;
import com.loong.seller.common.backend.param.DeliveryWaysQueryParam;
import com.loong.seller.common.backend.param.ShopDeliveryParam;
import com.loong.seller.handler.DeliveryWaysHandler;
import com.loong.seller.handler.ShopDeliveryHandler;
import com.loong.seller.model.ShopDelivery;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 配送方式控制
 * @Author: hzp
 * @Date: 2020-11-9 9:13
 */
@RestController
@RequestMapping("/manage/seller/deliveryWays")
public class DeliveryWaysController extends BaseBackendController {
    @Resource
    private DeliveryWaysHandler deliveryWaysHandler;
    @Resource
    private ShopDeliveryHandler shopDeliveryHandler;


    /**
     * 保存配送方式
     * @param param  配送方式
     * @return 200
     */
    @PostMapping("save")
    public Result save(@Validated(value = {CommonInsert.class, CommonValidate.class}) DeliveryWaysParam param){
        param.setId(null);
        return   handlerExecutor.execute(()->deliveryWaysHandler.save(param));
    }

    /**
     * 更新配送方式
     * @param param 配送方式
     * @return 200
     */
    @PostMapping("update")
    public Result update(@Validated(value = {CommonUpdate.class, CommonValidate.class}) DeliveryWaysParam param){
        return  handlerExecutor.execute(()->deliveryWaysHandler.update(param));
    }

    /**
     *根据id删除配送方式
     * @param ids 配送方式ids
     */
    @PostMapping("delete")
    public Result deleteById(@RequestParam List<Long> ids){
        //TODO 资源存在 判断
        return handlerExecutor.execute(()->deliveryWaysHandler.deleteByIds(ids));
    }

    /**
     *获取下拉列表的配送方式
     * @return
     */
    @PostMapping("findAll")
    public Result findAll(){
        return  handlerExecutor.execute(()->deliveryWaysHandler.findAllList());
    }

    /**
     * 根据店铺id查找店铺拥有的配送方式
     * @param shopId
     * @return
     */
    @PostMapping("shopDeliveryWays")
    public Result findDetailByShopId(Long shopId){
        return handlerExecutor.execute(()->deliveryWaysHandler.findDetailByShopId(shopId));
    }

    /**
     * 根据店铺id查找配送方式 只查有的没有的不显示
     * @param shopId
     * @return
     */
    @PostMapping("findDeliveryWays")
    public Result findDeliveryWaysByShop(Long shopId){
        return handlerExecutor.execute(()->deliveryWaysHandler.findDeliveryWaysByShopId(shopId));

    }

    /**
     *
     * 设置店铺配送方式的基础配送费
     * @param param
     * @return
     */
    @PostMapping("setShopDeliveryBasePrice")
    public Result setShopDeliveryBasePrice(@Validated(value = {CommonUpdate.class,CommonValidate.class})ShopDeliveryParam param){
        return handlerExecutor.execute(()->shopDeliveryHandler.update(TransformUtils.transform(param, ShopDelivery.class)));
    }

    /**
     *
     * 设置店铺配送方式默认选中
     * 现在默认只能设置一个 用于计算配送费 （只显示计算默认选择的配送方式的配送费）
     * @param param
     * @return
     */
    @PostMapping("setShopDeliverySelected")
    public Result setShopDeliverySelected(@Validated(value = {CommonUpdate.class})ShopDeliveryParam param){
        return handlerExecutor.execute(()->shopDeliveryHandler.setSelected(TransformUtils.transform(param, ShopDelivery.class)));
    }

    /**
     * 分页查询
     * @param baseQuery 分页对象
     * @param param 查询参数
     * @return
     */
    @PostMapping("pageQuery")
    public Result pageQuery(BaseQuery baseQuery, DeliveryWaysQueryParam param){

        return  handlerExecutor.execute(()->deliveryWaysHandler.pageQuery(baseQuery,param));
    }

}
